<?php

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use think\Request;

/**
 * @ControllerAnnotation(title="商品分类")
 */
class GoodCategory extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\GoodCategory();
        
        $pidMenuList = $this->model->getPidMenuList();
        $this->assign('pidMenuList', $pidMenuList);
        $this->assign('getStatusList', $this->model->getStatusList());
        $this->assign('pid', $this->request->param('id'));
    }

    /**
     * @NodeAnnotation(title="列表")
     */
    public function index() 
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if ($this->selectWhere) {
                $where = array_merge($where, $this->selectWhere);
            }
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)
            ->order(Db::raw('pid asc,sort =0,sort  asc'))->select();
            foreach ($list as &$item) {
                $item['id_title'] = '['.$item['id'].'] '.$item['title'];
            }

            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    
}